#include <assert.h>
#include <pthread.h>
#include <stdlib.h>

#include "base/simapi.h"

int g;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

#define NUM_OF_ITERATIONS		100

void* foo(void* in){
    int i;
    //    int tid = Sim_GetMyId();
    //pthread_barrier_wait(&barrier);
    for (i = 0; i < NUM_OF_ITERATIONS; i++){
        pthread_mutex_lock(&lock);
        g++;
	//	Sim_Print("Counter[%d]=%d!\n",tid,g);
        pthread_mutex_unlock(&lock);
    }
    return NULL;
}


void mainX(int argc, const char** const argv, const char **envp) {
    g = 0;
    int expectResult = NUM_OF_ITERATIONS * Sim_GetNumCpus();
    Sim_ParallelFork(&foo, NULL);
    if(g != expectResult){
        Sim_Print("Expects:  %d\n", expectResult);
        Sim_Print("Got:  %d\n", g);
        assert(0);
    }
    Sim_Print("Correct:  %d\n", g);
}
